from qwen_agent.agents import Assistant
from qwen_agent.utils.output_beautify import typewriter_print

# Step 1: Configure the LLM you are using.
llm_cfg = {
    'model': 'qwen3-32b',

    # Use the endpoint provided by Alibaba Model Studio:
    'model_type': 'qwen_dashscope',
    'api_key': "sk-0e687ddcf0164a6fb66c1096447223c4",

    # Use a custom endpoint compatible with OpenAI API:
    # 'model_server': 'http://localhost:8000/v1',  # api_base
    # 'api_key': 'EMPTY',

    # Other parameters:
    'generate_cfg': {
            # Add: When the response content is `<think>this is the thought</think>this is the answer;
            # Do not add: When the response has been separated by reasoning_content and content.
            #'thought_in_content': True,
            'top_p': 0.8
        },
}

# step2: Define Tools
tools = [
    {'mcpServers': {  # You can specify the MCP configuration file
            'time': {
                'command': 'uvx',
                'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
            },
            "fetch": {
                "command": "uvx",
                "args": ["mcp-server-fetch"]
            }
        }
    },
  'code_interpreter',  # Built-in tools
]

# Step 3: Create an agent. Here we use the `Assistant` agent as an example, which is capable of using tools and reading files.
system_instruction = '''After receiving the user's request, you should:
- first draw an image and obtain the image url,
- then run code `request.get(image_url)` to download the image,
- and finally select an image operation from the given document to process the image.
'''
# bot = Assistant(llm=llm_cfg, 
#                 system_message=system_instruction,
#                 function_list=tools)
bot = Assistant(llm=llm_cfg, function_list=tools)


# Step 4: Run the agent as a chatbot.
messages = []  # This stores the chat history.
while True:
    # For example, enter the query "draw a dog and rotate it 90 degrees".
    query = input('\nuser query: ')
    # Append the user query to the chat history.
    messages.append({'role': 'user', 'content': query})
    response = []
    response_plain_text = ''
    print('bot response:')
    for response in bot.run(messages=messages):
        # Streaming output.
        response_plain_text = typewriter_print(response, response_plain_text)
    # Append the bot responses to the chat history.
    messages.extend(response)